<title>编辑消息</title>

<div class="layui-fluid">
  <form class="layui-form" lay-filter="LAY_org_form">
    <div class="layui-row layui-col-space30" style="margin-top: 0px;" id="LAY_org_form_eidt_view"></div>
    <script type="text/html" id="LAY_org_form_tpl">
      <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
        <div class="LAY_wisdom_title">
          <h3 class="LAY_wisdom_title_line">基本信息</h3>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">机构名称</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.orgName || ''}}</div>
            {{# } else { }}
            <input type="text" name="orgName" lay-verify="required" placeholder="请输入" maxlength="32" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">机构简称</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.orgSimpleName || ''}}</div>
            {{# } else { }}
            <input type="text" name="orgSimpleName" lay-verify="required" placeholder="请输入" maxlength="16" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">机构类型</label>
          <div class="layui-input-block">

            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.orgCategoryDesc || ''}}</div>
            {{# } else { }}
            <select name="orgCategory" lay-verify="required" lay-filter="LAY_ategory">
              {{# if (d.category == 'ORG') { }}
              <option value="BROKER_ORG">经纪公司</option>
              <option value="TEAM">代理人团队</option>
              {{# } }}
              {{# if (d.category == 'CHANNEL' || d.category == 'ORG') { }}
              <option value="CHANNEL">渠道</option>
              {{# } }}
              {{# if (d.category == 'CHANNEL') { }}
              <option value="CHANNEL_SUB_ACC">渠道子账户</option>
              {{# } }}
            </select>
            {{# } }}
          </div>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">上级机构</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.parentOrgName || ''}}</div>
            {{# } else { }}
            <input type="text" name="parentOrgCode" lay-verify="required" id="LAY_treeSelect" lay-filter="LAY_treeSelect"
                   placeholder="请选择" readonly autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>

        {{# if (d.status == 'modify' && d.orgCategory == 'CHANNEL') { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">开发模式</label>
          <div class="layui-input-block">
            <div class="LAY_input_block">{{d.developModeDesc || ''}}</div>
          </div>
        </div>
        {{# } }}

        {{# if (d.status == 'modify' && d.orgCategory == 'CHANNEL') { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">备案状态</label>
          <div class="layui-input-block">
            <select name="putOnRecordStatus">
              <option value="0">备案中</option>
              <option value="1">已备案</option>
              <option value="2">无需备案</option>
            </select>
          </div>
        </div>
        {{# } }}

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">商户编码</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.merchantCode || ''}}</div>
            {{# } else { }}
            <input type="text" name="merchantCode" lay-verify="required|merchant" placeholder="请输入" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">联系电话</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify' && d.orgCategory == 'TEAM') { }}
            <div class="LAY_input_block">{{d.mobile || ''}}</div>
            {{# } else { }}
            <input type="text" name="mobile" lay-verify="required|phone" placeholder="请输入" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>

        {{# if (d.orgCategory == 'TEAM') { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">邀请码</label>
          <div class="layui-input-block">
            <input type="text" name="invitationCode" lay-verify="code" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        {{# } }}

        {{# if (d.status != 'modify') { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">惠易宝登录账号</label>
          <div class="layui-input-block">
            <input type="text" name="loginName" lay-verify="required|user" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">惠易宝登录密码</label>
          <div class="layui-input-block">
            <input type="password" name="loginPassword" lay-verify="required|password" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        {{# } }}
		
		<!-- added by lyuyang -->
		{{# if (d.orgCategory == 'BROKER_ORG') { }}
		
		<div class="LAY_wisdom_title">
          <h3 class="LAY_wisdom_title_line">工商注册信息</h3>
        </div>
		
		<div class="layui-form-item LAY_legalPerson" style="margin-top: 10px;">
          <label class="layui-form-label">法人</label>
          <div class="layui-input-block">
			{{# if (d.status == 'modify') { }}
			<div class="LAY_input_block">{{d.legalPerson || ''}}</div>
			{{# } else { }}
            <input type="text" name="legalPerson" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
			{{# } }}
          </div>
		</div>
		  
		<div class="layui-form-item LAY_legalPersonMobile" style="margin-top: 10px;">
		  <label class="layui-form-label">法人手机号码</label>
          <div class="layui-input-block">
			{{# if (d.status == 'modify') { }}
			<div class="LAY_input_block">{{d.legalPersonMobile || ''}}</div>
			{{# } else { }}
            <input type="text" name="legalPersonMobile" lay-verify="required|phone" placeholder="请输入" autocomplete="off" class="layui-input">
			{{# } }}
          </div>
		</div>
		  
		<div class="layui-form-item LAY_legalPersonPaperNo" style="margin-top: 10px;">
		  <label class="layui-form-label">法人证件号码</label>
          <div class="layui-input-block">
			{{# if (d.status == 'modify') { }}
			<div class="LAY_input_block">{{d.legalPersonPaperNo || ''}}</div>
			{{# } else { }}
            <input type="text" name="legalPersonPaperNo" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
			{{# } }}
          </div>
		</div>
		  
		<div class="layui-form-item LAY_unifiedSocialCreditCode" style="margin-top: 10px;">
		  <label class="layui-form-label">统一社会信用代码</label>
          <div class="layui-input-block">
			{{# if (d.status == 'modify') { }}
			<div class="LAY_input_block">{{d.unifiedSocialCreditCode || ''}}</div>
			{{# } else { }}
            <input type="text" name="unifiedSocialCreditCode" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
			{{# } }}
          </div>
		  </div>
        </div>
        {{# } }}
		<!-- added by lyuyang -->
		
      </div>
      <div class="layui-col-xs6 layui-col-sm6 layui-col-md6">
        <div class="LAY_wisdom_title">
          <h3 class="LAY_wisdom_title_line">结算账户信息</h3>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">是否独立核算</label>
          <div class="layui-input-block">
            {{# if (d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.independentAccountabilityDesc || ''}}</div>
            {{# } else { }}
            <select name="independentAccountability" lay-filter="LAY_independentAccountability" lay-verify="required">
              {{# if (d.orgCategory != 'CHANNEL_SUB_ACC') { }}
              <option value="1">是</option>
              {{# } }}

              {{# if (d.orgCategory == 'CHANNEL_SUB_ACC' || d.orgCategory == 'BROKER_ORG') { }}
              <option value="0">否</option>
              {{# } }}
            </select>
            {{# } }}
          </div>
        </div>

        {{# if (d.orgCategory != 'CHANNEL_SUB_ACC' && d.status == 'modify') { }}
        <div class="layui-form-item LAY_accountCategory" style="margin-top: 10px;">
          <label class="layui-form-label">账户类型</label>
          <div class="layui-input-block">
            <div class="LAY_input_block">{{d.accountCategoryDesc || ''}}</div>
          </div>
        </div>
        {{# } }}

        {{# if (d.orgCategory == 'TEAM') { }}
        <div class="layui-form-item LAY_teamLeaderMobile" style="margin-top: 10px;">
          <label class="layui-form-label">团长手机号</label>
          <div class="layui-input-block">
            <input type="text" name="teamLeaderMobile" lay-verify="required|phone" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        {{# } }}

        {{# if (d.orgCategory == 'BROKER_ORG' || d.orgCategory == 'CHANNEL') { }}
        {{# if (!(d.orgCategory == 'CHANNEL' && d.status == 'modify')) { }}
        <div class="layui-form-item LAY_bankName" style="margin-top: 10px;">
          <label class="layui-form-label">开户行</label>
          <div class="layui-input-block">
            <input type="text" name="bankName" lay-verify="account" placeholder="请输入" maxlength="64" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-form-item LAY_accountName" style="margin-top: 10px;">
          <label class="layui-form-label">账户名</label>
          <div class="layui-input-block">
            <input type="text" name="accountName" lay-verify="account" placeholder="请输入" maxlength="32" autocomplete="off" class="layui-input">
          </div>
        </div>
        {{# } }}

        <div class="layui-form-item LAY_accountNo" style="margin-top: 10px;">
          <label class="layui-form-label">账号</label>
          <div class="layui-input-block">
            {{# if (d.orgCategory == 'CHANNEL' && d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.accountNo || ''}}</div>
            {{# } else { }}
            <input type="text" name="accountNo" lay-verify="account" placeholder="请输入" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>
        {{# } }}


        {{# if (d.orgCategory == 'BROKER_ORG' || d.orgCategory == 'TEAM') { }}
        <div class="LAY_wisdom_title">
          <h3 class="LAY_wisdom_title_line">结佣账户信息</h3>
        </div>

        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">是否独立结佣</label>
          <div class="layui-input-block">
            {{# if (d.orgCategory == 'TEAM' && d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.independentBrokerageSettlementDesc || ''}}</div>
            {{# } else { }}
            <select name="independentBrokerageSettlement" id="LAY_brokerage" lay-filter="LAY_brokerage" lay-verify="required" lay-search>
              {{# if (d.orgCategory == 'BROKER_ORG') { }}
              <option value="1">是</option>
              {{# } }}
              <option value="0">否</option>
            </select>
            {{# } }}
          </div>
        </div>

        {{# if (!(d.orgCategory == 'TEAM' && d.status == 'modify' && d.independentBrokerageSettlement == 0)) { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">支付方式</label>
          <div class="layui-input-block">
            {{# if (d.orgCategory == 'TEAM' && d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.brokeragePayWayDesc || ''}}</div>
            {{# } else { }}
            <select name="brokeragePayWay" lay-verify="brokerage" lay-search>
              <option value="1">代付</option>
            </select>
            {{# } }}
          </div>
        </div>
        {{# } }}

        {{# if (!(d.orgCategory == 'TEAM' && d.status == 'modify' && d.independentBrokerageSettlement == 0)) { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">支付通道</label>
          <div class="layui-input-block">
            {{# if (d.orgCategory == 'TEAM' && d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.brokeragePayPassageDesc || ''}}</div>
            {{# } else { }}
            <select name="brokeragePayPassage" lay-verify="brokerage" lay-search>
              <option value="1">宝付</option>
            </select>
            {{# } }}
          </div>
        </div>
        {{# } }}

        {{# if (!(d.orgCategory == 'TEAM' && d.status == 'modify' && d.independentBrokerageSettlement == 0)) { }}
        <div class="layui-form-item" style="margin-top: 10px;">
          <label class="layui-form-label">商户号</label>
          <div class="layui-input-block">
            {{# if (d.orgCategory == 'TEAM' && d.status == 'modify') { }}
            <div class="LAY_input_block">{{d.payPassageMerchantNo || ''}}</div>
            {{# } else { }}
            <div class="LAY_input_block" id="LAY_merchant_false">委托独立核算上级进行结佣</div>
            <input type="text" id="LAY_merchant" name="payPassageMerchantNo" lay-verify="brokerage"
                   placeholder="填写内容请与技术部联系" autocomplete="off" class="layui-input">
            {{# } }}
          </div>
        </div>
        {{# } }}
        {{# } }}

        <button style="display: none;" id="LAY_org_form_submit" lay-submit lay-filter="*">提交</button>
      </div>
    </script>
  </form>
</div>

<style>
  html {
    background-color: #fff;
  }

  body.layui-layout-body {
    overflow-y: auto;
  }
</style>

<script>
  layui.use(['admin', 'form', 'laytpl', 'table', 'layer', 'laydate', 'layedit', 'util', 'treeSelect', 'md5'], function(){
    var $ = layui.$,
            admin = layui.admin,
            layer = layui.layer,
            setter = layui.setter,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            laytpl = layui.laytpl,
            util = layui.util,
            layedit = layui.layedit,
            treeSelect = layui.treeSelect,
            router = layui.router(),
            orgCode = router.search.orgCode,
            status = router.search.status,
            category = sessionStorage.category,
            formGroupValueCache;

    var orgCategory = 'CHANNEL';

    if (category == 'ORG') {
      orgCategory = 'BROKER_ORG';
    }

    initForm(orgCategory);

    function initForm(orgCategory) {
      if (orgCode == 'undefined') {
        orgCode = undefined
      }

      if (orgCode) {
        render(function (data) {
          initContent(data);
          if (formGroupValueCache) {
            form.val('LAY_org_form', formGroupValueCache)
          }
        });
      } else {
        initContent({ orgCategory: orgCategory });
        if (formGroupValueCache) {
          form.val('LAY_org_form', formGroupValueCache)
        }
      }

    }

    function initContent(content) {
      var buffer = $.extend(true, {}, {
        category: category,
        status: status
      }, content);

      renderData(buffer, function () {
        form.render(null, 'LAY_org_form');

        form.val('LAY_org_form', {
          orgCategory: buffer.orgCategory
        });

        form.verify({
          merchant: function (value, item) {
            if (!/^[0-9A-Za-z]{4,16}$/.test(value)) {
              return '必须为4到16位，字母（区分大小写）、数字以及它们的组合';
            }
          },
          code: function (value, item) {
            if (!/^[0-9A-Z]{6,6}$/.test(value)) {
              return '必须为6位，字母（大写）、数字以及它们的组合';
            }
          },
          user: function (value, item) {
            if (!/^[0-9A-Za-z_]{4,12}$/.test(value)) {
              return '必须为4到12位，字母（区分大小写）、数字、下划线以及它们的组合';
            }
          },
          password: function (value, item) {
            if (!/^(?!^[\d]+$)(?!^[_]+$)(?!^[a-zA-Z]+$)[\w-]{6,12}$/.test(value)) {
              return '6位-12位；至少同时包含字母、数字，下划线中的任意两种';
            }
          },
          account: function (value, item) {
            var formGroupValue = form.val('LAY_org_form');

            if (formGroupValue.independentAccountability == 1 && !value) {
              return '独立核算标识符为是时，该项不能为空';
            }
          },
          brokerage: function (value, item) {
            var formGroupValue = form.val('LAY_org_form');

            if (formGroupValue.independentBrokerageSettlement == 1 && !value) {
              return '独立结佣标识符为是时，该项不能为空';
            }
          }
        });

        form.on('select(LAY_brokerage)', function (data) {
          initPayPassageMerchantNo(data.value, buffer.orgCategory)
        });

        form.on('select(LAY_ategory)', function (data) {
          var formGroupValue = form.val('LAY_org_form');
          var formGroupValueCurrent = {
            orgName: formGroupValue.orgName,
            orgSimpleName: formGroupValue.orgSimpleName,
            orgCategory: formGroupValue.orgCategory,
            parentOrgCode: formGroupValue.parentOrgCode,
            putOnRecordStatus: formGroupValue.putOnRecordStatus,
            merchantCode: formGroupValue.merchantCode,
            mobile: formGroupValue.mobile,
            invitationCode: formGroupValue.invitationCode,
            loginName: formGroupValue.loginName,
            loginPassword: formGroupValue.loginPassword,
			
			legalPerson: formGroupValue.legalPerson,
			legalPersonMobile: formGroupValue.legalPersonMobile,
			legalPersonPaperNo: formGroupValue.legalPersonPaperNo,
			unifiedSocialCreditCode: formGroupValue.unifiedSocialCreditCode
          };
          formGroupValueCache = $.extend(true, formGroupValueCache || {}, formGroupValueCurrent);

          initForm(data.value);
        });

        form.on('select(LAY_independentAccountability)', function (data) {
          initIndependentAccountability(data.value)
        });

        form.on('submit(*)', function (data) {
          var field = data.field;
          var params = $.extend(true, {}, field);

          if (orgCode) {
            params = $.extend(true, {}, params, {
              orgCode: orgCode,
              orgCategory: buffer.orgCategory
            });
          }

          updataOrgInfo(params, function () {
            parent.refresh();
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
          });

          return false;
        });

        if (status == 'modify') {
          buffer.brokeragePayWay = buffer.brokeragePayWay || 1;
          buffer.brokeragePayPassage = buffer.brokeragePayPassage || 1


          form.val('LAY_org_form', buffer);
        } else {
          initTreeSelect(function () {
            if (formGroupValueCache && formGroupValueCache.parentOrgCode) {
              treeSelect.checkNode('LAY_treeSelect', formGroupValueCache.parentOrgCode);
            }
          });
        }

        var feild = form.val('LAY_org_form');
        initIndependentAccountability(feild.independentAccountability || buffer.independentAccountability);
        initPayPassageMerchantNo(feild.independentBrokerageSettlement || buffer.independentBrokerageSettlement, buffer.orgCategory);
      });
    }

    function initIndependentAccountability(value) {
      if (value == 0) {
        $('.LAY_teamLeaderMobile').hide();
        $('.LAY_accountCategory').hide();
        $('.LAY_bankName').hide();
        $('.LAY_accountName').hide();
        $('.LAY_accountNo').hide();
      } else {
        $('.LAY_teamLeaderMobile').show();
        $('.LAY_accountCategory').show();
        $('.LAY_bankName').show();
        $('.LAY_accountName').show();
        $('.LAY_accountNo').show();
      }
    }

    function initPayPassageMerchantNo(value, orgCategory) {
      if (orgCategory != 'BROKER_ORG' && orgCategory != 'TEAM') {
        return;
      }

      var targetMerchant = $('#LAY_merchant');
      var targetMerchantFalse = $('#LAY_merchant_false');

      if (value == 1) {
        targetMerchant.show();
        targetMerchantFalse.hide();
      } else {
        targetMerchant.hide();
        targetMerchantFalse.show();
      }
    }

    // 初始化插件
    function initTreeSelect(callback) {
      treeSelect.render({
        elem: '#LAY_treeSelect',
        data: setter.url + '/org/tree',
        type: 'GET',
        placeholder: '请选择',
        search: true,
        style: {
          folder: {
            enable: true
          },
          line: {
            enable: true
          }
        },
        success: function (d) {
          if (typeof(callback) == 'function') {
            callback(d);
          }
        }
      });
    }

    // 渲染数据
    function renderData(data, callback) {
      var getTpl = $('#LAY_org_form_tpl').html();

      if (!getTpl) {
        return;
      }

      laytpl(getTpl).render(data, function(html){
        $('#LAY_org_form_eidt_view').html(html);

        if (typeof(callback) === 'function') {
          callback();
        }
      });
    }

    // 回填信息
    function render(callback) {
      admin.req({
        url: setter.url + '/org/detail',
        type: 'GET',
        data: { orgCode: orgCode },
        done: function (res) {
          var data = res.data || {};

          if (typeof(callback) == 'function') {
            callback(data);
          }
        }
      });
    }

    // 升级机构信息
    function updataOrgInfo(params, callback) {
      var url = '/org/create';

      if (params.orgCode) {
        url = '/org/basicInfoUpdate'
      }

      var buffer = $.extend(true, {}, params);
      buffer.loginPassword = md5(buffer.loginPassword);

      admin.req({
        url: setter.url + url,
        type: 'POST',
        data: buffer,
        done: function (res) {
          var data = res.data;
          var msg = '操作失败';
          var icon = 2;

          if (data) {
            msg = '操作成功';
            icon = 1;
          }

          parent.layer.msg(msg, {
            icon: icon,
            time: 1500,
            shade: [0.3, '#393D49']
          });

          if (typeof(callback) == 'function') {
            callback(data);
          }
        }
      });
    }

  });
</script>
